package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService service;
    //还有权限属性未定义
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")QueryObject qo, Model model){
        model.addAttribute("result",this.service.query(qo));
        return "role/list";
    }
    @RequestMapping("/delete")
    public String delete(Long id){
        this.service.delete(id);
        return "redirect:/role/list.do";
    }

    @RequestMapping("/input")
    public String input(Long id,Model model){
        //查询权限的信息，显示在list.jsp页面中，一个方法
        model.addAttribute("permissions",permissionService.list());
        if(id!=null){
            model.addAttribute("entity",this.service.get(id));
        }
        return "role/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role entity,Long[] ids){
        this.service.saveOrUpdate(entity,ids);
        return "redirect:/role/list.do";
    }
}
